"""
运行所有 cProfile 示例
"""
import sys


def print_menu():
    """打印菜单"""
    print("\n" + "=" * 80)
    print("cProfile 性能分析示例")
    print("=" * 80)
    print("\n请选择要运行的示例:")
    print("  1. 基础使用示例 (basic_example.py)")
    print("  2. 高级功能示例 (advanced_example.py)")
    print("  3. GUI 性能分析示例 (gui_profiling.py)")
    print("  4. 可视化示例 (visualization.py)")
    print("  5. 运行所有示例")
    print("  0. 退出")
    print("-" * 80)


def run_basic_examples():
    """运行基础示例"""
    print("\n正在运行基础示例...")
    from basic_example import run_all_examples
    run_all_examples()


def run_advanced_examples():
    """运行高级示例"""
    print("\n正在运行高级示例...")
    from advanced_example import run_all_examples
    run_all_examples()


def run_gui_examples():
    """运行 GUI 示例"""
    print("\n正在运行 GUI 性能分析示例...")
    from gui_profiling import run_all_examples
    run_all_examples()


def run_visualization_examples():
    """运行可视化示例"""
    print("\n正在运行可视化示例...")
    from visualization import demo_all_visualizations, demo_html_export
    demo_all_visualizations()
    demo_html_export()


def run_all():
    """运行所有示例"""
    print("\n正在运行所有示例...")
    run_basic_examples()
    run_advanced_examples()
    run_gui_examples()
    run_visualization_examples()


def main():
    """主函数"""
    while True:
        print_menu()
        
        try:
            choice = input("\n请输入选项 (0-5): ").strip()
            
            if choice == '0':
                print("\n再见!")
                break
            elif choice == '1':
                run_basic_examples()
            elif choice == '2':
                run_advanced_examples()
            elif choice == '3':
                run_gui_examples()
            elif choice == '4':
                run_visualization_examples()
            elif choice == '5':
                run_all()
            else:
                print("\n无效的选项，请重新选择!")
            
            input("\n按回车键继续...")
            
        except KeyboardInterrupt:
            print("\n\n操作已取消!")
            break
        except Exception as e:
            print(f"\n发生错误: {e}")
            import traceback
            traceback.print_exc()
            input("\n按回车键继续...")


if __name__ == '__main__':
    main()
